{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import html\n",
    "import requests\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_to_csv(lyrics_data, file_name):\n",
    "    with open(file_name, mode='w', newline='', encoding='utf-8') as file:\n",
    "        writer = csv.writer(file)\n",
    "        writer.writerow(['歌名', '歌词'])  # 写入表头\n",
    "\n",
    "        for data in lyrics_data:\n",
    "            lyrics = ''.join(data['lyrics']).strip()  # 将所有歌词行合并为一个字符串\n",
    "            writer.writerow([data['title'], lyrics])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_numbers_from_file(file_path):\n",
    "    import re\n",
    "\n",
    "    pattern = re.compile(r'lyrics/(\\d+)')\n",
    "\n",
    "    numbers = []\n",
    "    with open(file_path, 'r') as file:\n",
    "        for line in file:\n",
    "            matches = pattern.findall(line)\n",
    "            numbers.extend(int(match) for match in matches)\n",
    "\n",
    "    return numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fetch_lyrics_data(numbers):\n",
    "    base_url = \"https://lyrics.net.cn/lyrics/\"\n",
    "    lyrics_data = []\n",
    "\n",
    "    for number in numbers:\n",
    "        url = base_url + str(number)\n",
    "        response = requests.get(url)\n",
    "\n",
    "        if response.status_code == 200:\n",
    "            tree = html.fromstring(response.content)\n",
    "            lyrics_content = tree.xpath('//*[@id=\"container\"]/div[2]//text()')\n",
    "            song_title = tree.xpath('//*[@id=\"container\"]/h2/text()')\n",
    "            lyrics_data.append({'title': song_title[0] if song_title else 'Unknown', 'lyrics': lyrics_content})\n",
    "        else:\n",
    "            print(f\"Failed to fetch data from {url}\")\n",
    "\n",
    "    return lyrics_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[27872,\n",
       " 27708,\n",
       " 923,\n",
       " 27856,\n",
       " 27842,\n",
       " 27741,\n",
       " 27735,\n",
       " 930,\n",
       " 27808,\n",
       " 27786,\n",
       " 27790,\n",
       " 27782,\n",
       " 27753,\n",
       " 27793,\n",
       " 27788,\n",
       " 928,\n",
       " 983,\n",
       " 961,\n",
       " 27738,\n",
       " 918,\n",
       " 1014,\n",
       " 931,\n",
       " 952,\n",
       " 1019,\n",
       " 27781,\n",
       " 27727,\n",
       " 929,\n",
       " 932,\n",
       " 964,\n",
       " 27734,\n",
       " 27771,\n",
       " 27713,\n",
       " 924,\n",
       " 953,\n",
       " 975,\n",
       " 1012,\n",
       " 27726,\n",
       " 920,\n",
       " 927,\n",
       " 1020,\n",
       " 27865,\n",
       " 27869,\n",
       " 27797,\n",
       " 27777,\n",
       " 27757,\n",
       " 27759,\n",
       " 27710,\n",
       " 936,\n",
       " 980,\n",
       " 992,\n",
       " 27806,\n",
       " 27720,\n",
       " 27712,\n",
       " 917,\n",
       " 925,\n",
       " 979,\n",
       " 27836,\n",
       " 27709,\n",
       " 954,\n",
       " 974,\n",
       " 976,\n",
       " 27867,\n",
       " 27784,\n",
       " 27838,\n",
       " 27825,\n",
       " 27791,\n",
       " 27767,\n",
       " 27714,\n",
       " 919,\n",
       " 970,\n",
       " 985,\n",
       " 27859,\n",
       " 27827,\n",
       " 27819,\n",
       " 27792,\n",
       " 27779,\n",
       " 27762,\n",
       " 27756,\n",
       " 27809,\n",
       " 27802,\n",
       " 27803,\n",
       " 27794,\n",
       " 27768,\n",
       " 27732,\n",
       " 911,\n",
       " 955,\n",
       " 977,\n",
       " 23901,\n",
       " 27841,\n",
       " 27795,\n",
       " 27765,\n",
       " 27754,\n",
       " 27715,\n",
       " 27723,\n",
       " 23911,\n",
       " 926,\n",
       " 962,\n",
       " 966,\n",
       " 978,\n",
       " 981,\n",
       " 984,\n",
       " 27860,\n",
       " 27861,\n",
       " 27839,\n",
       " 27823,\n",
       " 27769,\n",
       " 27725,\n",
       " 27718,\n",
       " 27711,\n",
       " 914,\n",
       " 960,\n",
       " 988,\n",
       " 994,\n",
       " 27871,\n",
       " 27799,\n",
       " 27717,\n",
       " 27721,\n",
       " 905,\n",
       " 934,\n",
       " 963,\n",
       " 965,\n",
       " 967,\n",
       " 973,\n",
       " 27824,\n",
       " 27807,\n",
       " 27780,\n",
       " 27755,\n",
       " 27731,\n",
       " 27716,\n",
       " 27719,\n",
       " 27722,\n",
       " 27724,\n",
       " 27707,\n",
       " 913,\n",
       " 933,\n",
       " 935,\n",
       " 982,\n",
       " 996,\n",
       " 1011]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file_path = './ZJ.html'\n",
    "numbers = extract_numbers_from_file(file_path)\n",
    "numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取数据\n",
    "data = fetch_lyrics_data(numbers)\n",
    "# 保存数据到 CSV 文件\n",
    "save_to_csv(data, 'lyrics_data_ZJ.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
